package com.keepers.repair.utils;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AlertDialog.Builder;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.SpanUtils;

/**
 * 弹出的对话框
 */
public class DialogUtil {

    /**
     * 无操作的对话框
     */
    public static void showDialogNoOperate(
            Context context, CharSequence title, CharSequence content) {
        if (isNotShow(context)) return;
        Builder builder = new Builder(context);
        builder.setTitle(title)
                .setMessage(content)
                .setPositiveButton("确定", null)
                .show();
    }

    /**
     * 确定对话框
     */
    public static void showDialogPositive(
            Context context, CharSequence title, CharSequence content,
            DialogInterface.OnClickListener positiveListener) {
        if (isNotShow(context)) return;
        Builder builder = new Builder(context);
        builder.setTitle(title)
                .setMessage(content)
                .setPositiveButton("确定", positiveListener)
                .show();
    }

    /**
     * 确定对话框
     */
    public static void showDialogPositiveWithBtnText(
            Context context, CharSequence title, CharSequence content,
            String btnText, DialogInterface.OnClickListener positiveListener) {
        if (isNotShow(context)) return;
        Builder builder = new Builder(context);
        builder.setTitle(title)
                .setMessage(content)
                .setPositiveButton(btnText, positiveListener)
                .show();
    }

    /**
     * 确定对话框点击外部不可消失
     */
    public static void showDialogPositiveUnCancel(
            Context context, CharSequence title, CharSequence content,
            DialogInterface.OnClickListener positiveListener) {
        if (isNotShow(context)) return;
        Builder builder = new Builder(context);
        Dialog dialog = builder
                .setTitle(title)
                .setMessage(content)
                .setPositiveButton("确定", positiveListener)
                .setCancelable(false)
                .create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    /**
     * 使用自定义视图
     */
    public static void showWithView(
            Context context, CharSequence title, int viewId,
            String positiveTitle, DialogInterface.OnClickListener positiveListener) {
        if (context == null || viewId < 1) {
            return;
        }
        if (isNotShow(context)) return;
        Builder builder = new Builder(context);
        builder.setTitle(title)
                .setView(viewId)
                .setPositiveButton(positiveTitle, positiveListener)
                .show();
    }

    /**
     * 显示普通对话框
     */
    public static void showCommonDialog(
            Context context, CharSequence title, CharSequence content,
            DialogInterface.OnClickListener positiveListener,
            DialogInterface.OnClickListener negativeListener) {
        showCommonDialog(context,
                title, content,
                "确定", positiveListener,
                "取消", negativeListener);
    }


    public static void showCommonDialog(
            Context context, CharSequence title, CharSequence content,
            CharSequence positiveTitle, DialogInterface.OnClickListener positiveListener,
            CharSequence negativeTitle, DialogInterface.OnClickListener negativeListener) {
        if (isNotShow(context)) return;
        Builder builder = new Builder(context);
        builder.setTitle(title)
                .setMessage(content)
                .setPositiveButton(positiveTitle, positiveListener)
                .setNegativeButton(getNegativeStr(negativeTitle), negativeListener)
                .show();
    }

    public static void showCommonDialogUnCancel(
            Context context, CharSequence title, CharSequence content,
            DialogInterface.OnClickListener positiveListener) {
        showCommonDialogUnCancel(context,
                title, content,
                "确定", positiveListener);
    }

    public static void showCommonDialogUnCancel(
            Context context, CharSequence title, CharSequence content,
            CharSequence positiveTitle, DialogInterface.OnClickListener positiveListener) {
        if (isNotShow(context)) return;
        Builder builder = new Builder(context);
        Dialog dialog = builder.setTitle(title)
                .setMessage(content)
                .setPositiveButton(positiveTitle, positiveListener)
                .setCancelable(false)
                .create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    public static void showCommonDialogUnCancel(
            Context context, CharSequence title, CharSequence content,
            DialogInterface.OnClickListener positiveListener,
            DialogInterface.OnClickListener negativeListener) {
        showCommonDialogUnCancel(context,
                title, content,
                "确定", positiveListener,
                "取消", negativeListener);
    }

    public static void showCommonDialogUnCancel(
            Context context, CharSequence title, CharSequence content,
            CharSequence positiveTitle, DialogInterface.OnClickListener positiveListener,
            CharSequence negativeTitle, DialogInterface.OnClickListener negativeListener) {
        if (isNotShow(context)) return;
        Builder builder = new Builder(context);
        AlertDialog alertDialog = builder.setTitle(title)
                .setMessage(content)
                .setPositiveButton(positiveTitle, positiveListener)
                .setNegativeButton(getNegativeStr(negativeTitle), negativeListener)
                .setCancelable(false)
                .create();
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.show();
    }


    public static void configCenterDialog(Dialog dialog) {
        configCenterDialog(dialog, 280);
    }

    public static void configCenterDialog(Dialog dialog, int widthDP) {
        Window dialogWindow = dialog.getWindow();
        if (dialogWindow == null) return;
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER);
        if (widthDP > 0) {
            lp.width = SizeUtils.dp2px(widthDP);
        } else {
            lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        }
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

    }

    private static boolean isNotShow(Context context) {
        return context == null || ((Activity) context).isFinishing();
    }

    /**
     * 获取灰色的取消按钮
     */
    private static CharSequence getNegativeStr(CharSequence str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }

        return new  SpanUtils()
                .append(str)
                .setForegroundColor(Color.parseColor("#626262"))
                .create();
    }

    public static ProgressDialog getProgressDialog(Context context, String message) {
        ProgressDialog progressDialog = new ProgressDialog(context);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // 设置圆形进度条
        progressDialog.setCancelable(false);
        progressDialog.setMessage(message);
        return progressDialog;
    }
}
